package com.jmu.edu.IMApplication.myview;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.google.android.material.button.MaterialButton;
import com.jmu.edu.IMApplication.R;

public class EditDialog extends AlertDialog {
    private Context context;
    private MaterialButton confirmButton, cancelButton;
    private TextView titleText;
    private EditText inputText;

    private String title;
    private View.OnClickListener onConfirmClickListener;
    private View.OnClickListener onCancelClickListener;


    public EditDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public EditDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_edit);
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        initView();
    }


    private void initView() {
        confirmButton = findViewById(R.id.dialog_edit_confirm);
        cancelButton = findViewById(R.id.dialog_edit_cancel);
        titleText = findViewById(R.id.dialog_edit_title);
        inputText = findViewById(R.id.dialog_edit_input);
        // 需要设置以下两个属性才能正确在editText中进行输入
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        fillViewWithValues();
        setListener();
    }

    private void fillViewWithValues() {
        titleText.setText(title);

    }

    private void setListener() {
        // 如果没有监听，那么监听默认为关闭该弹窗
        if (onConfirmClickListener != null) {
            confirmButton.setOnClickListener(onConfirmClickListener);
        } else {
            confirmButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
        // 如果没有监听，那么监听默认为关闭该弹窗
        if (onCancelClickListener != null) {
            cancelButton.setOnClickListener(onCancelClickListener);
        } else {
            cancelButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }

    /**
     * 如果在创建完以后还想要更改对应的属性，那么需要在设置完属性后调用update方法
     * update方法重新调用setListener来设置监听,调用fillViewWithValues来设置title
     */
    public void update() {
        fillViewWithValues();
        setListener();
    }

    public String getInputText() {
        return inputText.getText().toString().trim();
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public View.OnClickListener getOnConfirmClickListener() {
        return onConfirmClickListener;
    }

    public void setOnConfirmClickListener(View.OnClickListener onConfirmClickListener) {
        this.onConfirmClickListener = onConfirmClickListener;
    }

    public View.OnClickListener getOnCancelClickListener() {
        return onCancelClickListener;
    }

    public void setOnCancelClickListener(View.OnClickListener onCancelClickListener) {
        this.onCancelClickListener = onCancelClickListener;
    }
}
